package com.jingtai.shop.common.utils.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jingtai.common.constants.RedisConstant;
import com.jingtai.shop.common.utils.HttpUtils;
import com.jingtai.shop.common.config.sms.SmsConfig;
import com.jingtai.shop.common.constants.SendMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class SendMessageUtils {

    @Resource
    private SmsConfig smsConfig;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    public boolean sendMessageToRegister(String phone,String code){

        String content="【仁仁科技】您本次的操作的验证码是:" + code + ",3分钟有效。如非本人操作，请忽略本条短信";

        SendMessage message = new SendMessage();
        message.setAction(smsConfig.getSend());
        message.setUserid(smsConfig.getUserId());
        message.setAccount(smsConfig.getAccount());
        message.setPassword(smsConfig.getPassword());
        message.setMobile(phone);
        message.setContent(content);
        message.setSendtime("");
        message.setExtno("");

        Map map = JSON.parseObject(JSON.toJSONString(message), Map.class);
        String result = HttpUtils.doPost(smsConfig.getUrl(), map);
        if (StringUtils.isNotBlank(result)){
            JSONObject jsonObject = JSON.parseObject(result);
            if (StringUtils.equals("Success", jsonObject.getString("returnstatus"))){
                redisTemplate.opsForValue().set(RedisConstant.SMS_VERIFICATION_CODE_REGISTER + phone, code
                        , 3, TimeUnit.MINUTES);
                log.info("短信内容放入redis中:key:[{}],value:[{}]", RedisConstant.SMS_VERIFICATION_CODE_REGISTER + phone,
                        content);
                return true;
            }
        }
        return false;
    }

    public boolean sendMessageToForgetpwd(String phone,String code){

        String content="【仁仁科技】您本次的操作的验证码是:" + code + ",3分钟有效。如非本人操作，请忽略本条短信";

        SendMessage message = new SendMessage();
        message.setAction(smsConfig.getSend());
        message.setUserid(smsConfig.getUserId());
        message.setAccount(smsConfig.getAccount());
        message.setPassword(smsConfig.getPassword());
        message.setMobile(phone);
        message.setContent(content);
        message.setSendtime("");
        message.setExtno("");

        Map map = JSON.parseObject(JSON.toJSONString(message), Map.class);
        String result = HttpUtils.doPost(smsConfig.getUrl(), map);
        if (StringUtils.isNotBlank(result)){
            JSONObject jsonObject = JSON.parseObject(result);
            if (StringUtils.equals("Success", jsonObject.getString("returnstatus"))){
                redisTemplate.opsForValue().set(RedisConstant.SMS_VERIFICATION_CODE_FORGETPWD + phone, code
                        , 3, TimeUnit.MINUTES);
                log.info("短信内容放入redis中:key:[{}],value:[{}]", RedisConstant.SMS_VERIFICATION_CODE_FORGETPWD + phone,
                        content);
                return true;
            }
        }
        return false;
    }
}
